package LRUCache;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author wtcode
 * @date 2021/3/30 - 8:04
 */
public class LRUCacheDemo extends LinkedHashMap{
    // 缓存坑位
    private int capacity;

    // true 按访问顺序
    // false 按插入顺序
    public LRUCacheDemo(int capacity) {
        super(capacity,0.75f,true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return super.size() > capacity;


    }

    public static void main(String[] args) {

        LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3);
        lruCacheDemo.put(1,"a");
        lruCacheDemo.put(2,"b");
        lruCacheDemo.put(3,"c");

        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(4,"d");
        System.out.println(lruCacheDemo.keySet());
    }

}
